package com.cy.ppj.web.admin.mall;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.mall.BrandAddAO;
import com.cy.ppj.model.ao.mall.BrandEditAO;
import com.cy.ppj.model.domian.mall.Brand;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.BrandAdminListQuery;
import com.cy.ppj.model.vo.mall.BrandAdminDetailVO;
import com.cy.ppj.model.vo.mall.BrandAdminListVO;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.mall.BrandService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Description 后台-品牌管理
 * @Author tanyilin
 * @Date 2019/7/2 15:31
 */
@RestController
@RequestMapping("/admin/mall/brand")
@Api(tags = "后台-品牌管理")
public class AdminBrandController extends BaseController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private FileService fileService;

    @ApiOperation("品牌列表")
    @GetMapping("/list")
    public PageBean<BrandAdminListVO> getBrandList(BrandAdminListQuery query) {

        PageBean<BrandAdminListVO> pageBean = brandService.getBrandAdminList(query);
        return pageBean;
    }

    @ApiOperation("品牌详情")
    @GetMapping("/detail")
    public Result<BrandAdminDetailVO> getBrandDetail(@RequestParam(value = "brandId") Long brandId) {

        BrandAdminDetailVO detailVO = brandService.getBrandAdminDetail(brandId);
        return ok("获取详情成功", detailVO);
    }

    @ApiOperation("上下架状态")
    @PostMapping("/update/status")
    public Result updateBrandStatus(@RequestParam(value = "brandId") Long brandId, @RequestParam(value = "status") CommonStatus status) {

        Brand updateBrand = new Brand();
        updateBrand.setId(brandId);
        updateBrand.setStatus(status);
        brandService.updateBrand(updateBrand);

        return ok("更新状态成功");
    }

    @ApiOperation("新增品牌")
    @PostMapping("/do/add")
    public Result doAddBrand(@Validated BrandAddAO addAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Brand insertBrand = new Brand();
        BeanUtils.copyProperties(addAO, insertBrand);
        insertBrand.setLogo(addAO.getLogoPath());
        insertBrand.setDisplayImage(addAO.getBannerPath());
        insertBrand.setDetailImage(addAO.getBannerPath());
        brandService.addBrand(insertBrand);

        return ok("新增品牌成功");
    }

    @ApiOperation("编辑品牌")
    @PostMapping("/do/edit")
    public Result doEditBrand(@Validated BrandEditAO editAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Brand updateBrand = new Brand();
        BeanUtils.copyProperties(editAO, updateBrand);
        updateBrand.setId(editAO.getBrandId());
        updateBrand.setLogo(editAO.getLogoPath());
        updateBrand.setDisplayImage(editAO.getBannerPath());
        updateBrand.setDetailImage(editAO.getBannerPath());
        brandService.updateBrand(updateBrand);

        return ok("编辑品牌成功");
    }

    @ApiOperation("上传品牌图片")
    @PostMapping("/upload/image")
    public Result uploadImage(@RequestParam(value = "file") MultipartFile file) {

        String directory = "brand/";
        Result<Map<String, String>> uploadResult = fileService.uploadToMallBucket(file, directory, null);
        return uploadResult;
    }

    @ApiOperation("全部的品牌列表，不分页")
    @GetMapping("/get/all/list")
    public Result<List<Brand>> getAllBrandList() {
        return ok("获取成功", brandService.getAllBrandList());
    }

}
